import java.io.Serializable;
import java.util.Random;

/**
 * represents the LandOffice where Properties can be bought by different Player objects
 * located inside of the Town
 * @author Sammy Warrayat
 *
 */
public class LandOffice implements Serializable{
	
	private Random r;
	private GameModel gm;
/*
 * The land office buys and sells property. If a player wishes to sell a property 
 * during their turn, they may enter the land office and offer a property to sell. 
 * If a mule is on the property when sold, it is lost. 
 * The selling price is 400 + random(0-200). 
 * You may also buy property if any is available from the land office. 
 * The buying price is 300 + round * random(0-100).
 */
	/**
	 * 
	 */
	public LandOffice(){
		r = new Random();
		gm = GameModel.getInstance();
	}
	/**
	 * Checks if player can afford a property
	 * @param p
	 * @param prop
	 * @return
	 */
	public boolean canAfford(Player p, int cost){
		return (p.getMoney() > cost);
	}
	/**
	 * buys a property from the player, changing the property's status
	 * from owned to not owned and increasing the player's money using a formula
	 * 
	 * @param p
	 * @param prop
	 * @return
	 */
	public boolean buyProp(Player p, Property prop){
		gm = GameModel.getInstance();
		int cost = 300 + (gm.getRound().getRoundNum() * r.nextInt(100));
		prop.setOwner(null);
		p.setMoney(p.getMoney() + cost);
		prop.setMule(null);
		return true;
		
	}
	/**
	 * sells a property to an owner, changing the property's status 
	 * from not owned to owned and decreases the player's money using a formula
	 * 
	 * @param p
	 * @param prop
	 * @return
	 */
	public boolean sellProp(Player p, Property prop){
		int cost = 400 + r.nextInt(200);
		if (canAfford(p, cost) && prop.getOwner() == null){
			prop.setOwner(p);
			p.setMoney(p.getMoney() - cost);
			return true;
		}
		return false;
	}

}
